/*
 * Copyright (C) 2012-2013 Michael L.R. Marques
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact: michaellrmarques@gmail.com
 */
package com.jm.jmsql.database;

import com.jm.commons.logging.LogSystem;
import com.jm.commons.utils.Constants;
import com.jm.jmsql.JMSql;
import com.jm.jmsql.models.DBXploraTreeModel;
import com.jm.jmsql.objects.Catalog;
import com.jm.jmsql.objects.Column;
import com.jm.jmsql.objects.Database;
import com.jm.jmsql.objects.Databases;
import com.jm.jmsql.objects.Definition;
import com.jm.jmsql.objects.Procedure;
import com.jm.jmsql.objects.ProcedureColumn;
import com.jm.jmsql.objects.Procedures;
import com.jm.jmsql.objects.Schema;
import com.jm.jmsql.objects.Table;
import com.jm.jmsql.objects.Tables;
import com.jm.jmsql.objects.View;
import com.jm.jmsql.objects.Views;
import com.jm.jmsql.objects.base.JMSqlObject;
import com.jm.jmsql.objects.events.DefinitionEvent;
import com.jm.jmsql.objects.events.DefinitionTestConnectionAdapter;
import com.jm.jmsql.sql.SQLEditor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTree;

/**
 *
 * @created Nov 23, 2012
 * @author Michael
 */
public class DBXploraPopupMenu extends JPopupMenu {
    
    /**
     * 
     * @param app
     * @param tree
     * @param x
     * @param y 
     */
    public static void show(final JMSql app, final JTree tree, int x, int y) {
        DBXploraPopupMenu popup = new DBXploraPopupMenu();
        if (tree.getLastSelectedPathComponent() instanceof JMSqlObject) {
            // Check which type of selected component is selected
            if (tree.getLastSelectedPathComponent() instanceof Databases) {
                final Databases databases = (Databases) tree.getLastSelectedPathComponent();
                // Create a database
                JMenuItem createDatabase = new JMenuItem("Create Database...");
                createDatabase.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/create_database.png")));
                createDatabase.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        DatabaseManager driverManager = new DatabaseManager(app);
                        if (DatabaseManager.SUCCESSFUL == driverManager.showDialog()) {
                            Database database = driverManager.getDatabase();
                            if (database != null) {
                                if (databases.add(database)) {
                                    ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(database.getParent());
                                    JOptionPane.showMessageDialog(app, "Successfully added a database", "Add Database", JOptionPane.INFORMATION_MESSAGE);
                                }
                            }
                        }
                    }
                });
                // Add the options to the popup menu
                popup.add(createDatabase);
            } else if (tree.getLastSelectedPathComponent() instanceof Database) {
                final Database database = (Database) tree.getLastSelectedPathComponent();
                // Customize the selected database
                JMenuItem customizeDatabase = new JMenuItem("Customize Database...");
                customizeDatabase.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/database.png")));
                customizeDatabase.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        DatabaseManager driverManager = new DatabaseManager(app);
                        driverManager.setDatabase(database);
                        if (DatabaseManager.SUCCESSFUL == driverManager.showDialog()) {
                            Databases databases = (Databases) database.getParent();
                            Database newDatabase = driverManager.getDatabase();
                            if (newDatabase != null) {
                                if (databases.set(database, newDatabase)) {
                                    ((DBXploraTreeModel) tree.getModel()).fireTreeNodesChanged(newDatabase);
                                    JOptionPane.showMessageDialog(app, "Successfully changed the selected database", "Customize Database", JOptionPane.INFORMATION_MESSAGE);
                                }
                            }
                        }
                    }
                });
                // Remove the selected database
                JMenuItem removeDatabase = new JMenuItem("Remove Database");
                removeDatabase.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/delete_database.png")));
                removeDatabase.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        if (((Databases) database.getParent()).remove(database)) {
                            ((DBXploraTreeModel) tree.getModel()).fireTreeNodesRemoved(database);
                            JOptionPane.showMessageDialog(app, "Successfully removed the selected database", "Remove Database", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                });
                // Create a definition
                JMenuItem createDefinition = new JMenuItem("Create Definition...");
                createDefinition.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/create_definition.png")));
                createDefinition.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        DefinitionManager definitionManager = new DefinitionManager(app, database);
                        if (DefinitionManager.SUCCESSFUL == definitionManager.showDialog()) {
                            Definition definition = definitionManager.getDefinition();
                            if (definition != null) {
                                if (database.add(definitionManager.getDefinition())) {
                                    ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(definition.getParent());
                                    JOptionPane.showMessageDialog(app, "Successfully added a definition", "Add Definition", JOptionPane.INFORMATION_MESSAGE);
                                }
                            }
                        }
                    }
                });
                // Add the options to the popup menu
                popup.add(customizeDatabase);
                popup.add(removeDatabase);
                popup.add(new JSeparator());
                popup.add(createDefinition);
            } else if (tree.getLastSelectedPathComponent() instanceof Definition) {
                final Definition definition = (Definition) tree.getLastSelectedPathComponent();
                final Database database = definition.getDatabase();
                // Create a connection to the database
                JMenuItem connect = new JMenuItem("Connect");
                connect.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/connected_definition.png")));
                connect.setEnabled(!definition.isConnected());
                connect.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        definition.addDefinitionListener(new DefinitionTestConnectionAdapter() {
                            @Override
                            public void connectionSuccessful(DefinitionEvent e) {}
                            @Override
                            public void connectionFailed(DefinitionEvent e) {
                                JOptionPane.showMessageDialog(app, "Datasource: " + definition.getDatasource() + Constants.NEW_LINE + "Username: " + definition.getUsername() + Constants.NEW_LINE + definition.getFullException(), "Connection Error", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                        definition.connect();
                    }
                });
                // Close the connection to the database
                JMenuItem disconnect = new JMenuItem("Disconnect");
                disconnect.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/disconnected_definition.png")));
                disconnect.setEnabled(definition.isConnected());
                disconnect.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        if (!definition.disconnect()) {
                            JOptionPane.showMessageDialog(app, definition.getFullException(), "Disconnect Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                });
                // Customize the selected definition
                JMenuItem customizeDefinition = new JMenuItem("Customize Definition...");
                customizeDefinition.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/connected_definition.png")));
                customizeDefinition.setEnabled(!definition.isConnected());
                customizeDefinition.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        DefinitionManager definitionManager = new DefinitionManager(app, definition.getDatabase());
                        definitionManager.setDefinition(definition);
                        if (DefinitionManager.SUCCESSFUL == definitionManager.showDialog()) {
                            Database database = definition.getDatabase();
                            Definition newDefinition = definitionManager.getDefinition();
                            if (newDefinition != null) {
                                if (database.set(definition, newDefinition)) {
                                    ((DBXploraTreeModel) tree.getModel()).fireTreeNodesChanged(newDefinition);
                                    JOptionPane.showMessageDialog(app, "Successfully changed the selected definition", "Customize Definition", JOptionPane.INFORMATION_MESSAGE);
                                }
                            }
                        }
                    }
                });
                // Customize the selected definition
                JMenuItem removeDefinition = new JMenuItem("Remove Definition");
                removeDefinition.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/delete_definition.png")));
                removeDefinition.setEnabled(!definition.isConnected());
                removeDefinition.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Database database = definition.getDatabase();
                        if (database.remove(definition)) {
                            ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(database);
                            JOptionPane.showMessageDialog(app, "Successfully removed the selected definition", "Remove Definition", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                });
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        definition.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(definition);
                    }
                });
                // Execute a query on the selected database
                JMenuItem executeQuery = new JMenuItem("Execute Query");
                executeQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/play.png")));
                executeQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        SQLEditor editor = new SQLEditor((Databases) ((Database) definition.getParent()).getParent(), definition);
                        app.newSQLEditor("Execute Query.sql", editor);
                    }
                });
                // Add the options to the popup menu
                if (database.isDriverLoaded()) {
                    popup.add(connect);
                    popup.add(disconnect);
                    popup.add(new JSeparator());
                }
                popup.add(customizeDefinition);
                popup.add(removeDefinition);
                popup.add(new JSeparator());
                popup.add(refresh);
                popup.add(new JSeparator());
                popup.add(executeQuery);
            } else if (tree.getLastSelectedPathComponent() instanceof Catalog) {
                final Catalog catalog = (Catalog) tree.getLastSelectedPathComponent();
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        catalog.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(catalog);
                    }
                });
                // Add these options to the popup menu
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof Schema) {
                final Schema schema = (Schema) tree.getLastSelectedPathComponent();
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        schema.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(schema);
                    }
                });
                // Add these options to the popup menu
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof Tables) {
                final Tables tables = (Tables) tree.getLastSelectedPathComponent();
                // Create table dialog box
                JMenuItem createTable = new JMenuItem("Create Table");
                createTable.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/create_table.png")));
                createTable.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        CreateTable create = new CreateTable(app, tables);
                        create.setVisible(true);
                        tables.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(tables);
                    }
                });
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        tables.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(tables);
                    }
                });
                // Add these options to the popup menu
                popup.add(createTable);
                popup.add(new JSeparator());
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof Procedures) {
                final Procedures procedures = (Procedures) tree.getLastSelectedPathComponent();
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        procedures.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(procedures);
                    }
                });
                // Add these options to the popup menu
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof Views) {
                final Views views = (Views) tree.getLastSelectedPathComponent();
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        views.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(views);
                    }
                });
                // Add these options to the popup menu
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof View) {
                final View view = (View) tree.getLastSelectedPathComponent();
                // View the table by executing an sql query
                JMenuItem viewData = new JMenuItem("View Data");
                viewData.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/view.png")));
                viewData.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Schema schema = (Schema) ((Views) view.getParent()).getParent();
                        Definition definition = (Definition) schema.getParent();
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("View " + view.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, "SELECT *\nFROM " + (schema.getName() != null ? schema.getName() + "." : "") + view.getName(), true));
                    }
                });
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        view.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(view);
                    }
                });
                // Add these options to the popup menu
                popup.add(viewData);
                popup.add(new JSeparator());
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof Procedure) {
            } else if (tree.getLastSelectedPathComponent() instanceof Table) {
                final Table table = (Table) tree.getLastSelectedPathComponent();
                // View the table by executing an sql query
                JMenuItem viewData = new JMenuItem("View Table Data");
                viewData.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table.png")));
                viewData.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Select " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.selectSQLQuery(), true));
                    }
                });
                // Delete the table by executing an sql query
                JMenuItem delete = new JMenuItem("Delete Table");
                delete.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/delete_table.png")));
                delete.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(app, "Are you sure you would like to delete this table?", "Delete Table " + table.getName(), JOptionPane.YES_NO_OPTION)) {
                            Tables tables = (Tables) table.getParent();
                            Definition definition = null;
                            if (tables.getParent() instanceof Definition) {
                                definition = (Definition) tables.getParent();
                            } else {
                                definition = (Definition) tables.getParent().getParent();
                            }
                            
                            Database database = (Database) definition.getParent();
                            app.newSQLEditor("Delete " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.dropSQLQuery(), true));
                            tables.reload();
                            ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(tables);
                        }
                    }
                });
                // View the table select query
                JMenuItem selectQuery = new JMenuItem("Select SQL Query");
                selectQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table.png")));
                selectQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Select " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.selectSQLQuery(), false));
                    }
                });
                // View the table insert query
                JMenuItem insertQuery = new JMenuItem("Insert SQL Query");
                insertQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table.png")));
                insertQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Insert " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.insertSQLQuery(), false));
                    }
                });
                // View the table select query
                JMenuItem updateQuery = new JMenuItem("Update SQL Query");
                updateQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table.png")));
                updateQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Update " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.updateSQLQuery(), false));
                    }
                });
                // View the table select query
                JMenuItem deleteQuery = new JMenuItem("Delete SQL Query");
                deleteQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table.png")));
                deleteQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Delete " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, table.deleteSQLQuery(), false));
                    }
                });
                // View the table's structure in SQL
                JMenuItem createQuery = new JMenuItem("Create Table SQL Query");
                createQuery.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/table_structure.png")));
                createQuery.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Tables tables = (Tables) table.getParent();
                        Definition definition = null;
                        if (tables.getParent() instanceof Definition) {
                            definition = (Definition) tables.getParent();
                        } else {
                            definition = (Definition) tables.getParent().getParent();
                        }
                        Database database = (Database) definition.getParent();
                        try {
                            app.newSQLEditor("Create " + table.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, definition.getConnection().nativeSQL(table.createSQLQuery()), false));
                        } catch (SQLException sqle) {
                            LogSystem.log(getClass(), getClass().getEnclosingMethod(), "Build the tables SQL structure", sqle);
                        }
                    }
                });
                // Refresh option
                JMenuItem refresh = new JMenuItem("Refresh");
                refresh.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/refresh.png")));
                refresh.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        table.reload();
                        ((DBXploraTreeModel) tree.getModel()).fireStructureChanged(table);
                    }
                });
                // Add these options to the popup menu
                popup.add(viewData);
                popup.add(delete);
                popup.add(new JSeparator());
                popup.add(selectQuery);
                popup.add(insertQuery);
                popup.add(updateQuery);
                popup.add(deleteQuery);
                popup.add(createQuery);
                popup.add(new JSeparator());
                popup.add(refresh);
            } else if (tree.getLastSelectedPathComponent() instanceof ProcedureColumn) {
            } else if (tree.getLastSelectedPathComponent() instanceof Column) {
                final Column column = (Column) tree.getLastSelectedPathComponent();
                // View all the data in this column by executing an sql query
                JMenuItem columnField = new JMenuItem("View Column Data");
                columnField.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/database.png")));
                columnField.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        Table table = (Table) column.getParent();
                        Schema schema = (Schema) ((Tables) table.getParent()).getParent();
                        Definition definition = (Definition) schema.getParent();
                        Database database = (Database) definition.getParent();
                        app.newSQLEditor("Select " + column.getName() + ".sql", new SQLEditor((Databases) database.getParent(), definition, "SELECT " + column.getName() + "\nFROM " + schema.getName() + "." + table.getName(), true));
                    }
                });
                // Add these options to the popup menu
                popup.add(columnField);
            }
            // Sort children objects
            JMenuItem sort = new JMenuItem("Sort...");
            sort.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/sort.png")));
            sort.addActionListener(new ActionListener() {
                @Override public void actionPerformed(ActionEvent e) {
                    Collections.sort((JMSqlObject) tree.getLastSelectedPathComponent());
                    ((DBXploraTreeModel) tree.getModel()).fireTreeNodesChanged((JMSqlObject) tree.getLastSelectedPathComponent());
                }
            });
            // View the properties of the selected column
            JMenuItem properties = new JMenuItem("Properties");
            properties.setIcon(new ImageIcon(app.getClass().getClassLoader().getResource("com/jm/jmsql/icons/database_properties.png")));
            properties.addActionListener(new ActionListener() {
                @Override public void actionPerformed(ActionEvent e) {
                    Properties.showDialog(app, (JMSqlObject) tree.getLastSelectedPathComponent());
                }
            });
            // Check if we can sort children objects
            if (((JMSqlObject) tree.getLastSelectedPathComponent()).isSortable()) {
                if (popup.getComponentCount() > 0) {
                    popup.add(new JSeparator());
                }
                popup.add(sort);
            }
            // Check if their are already items and add separator
            if (popup.getComponentCount() > 0) {
                popup.add(new JSeparator());
            }
            popup.add(properties);
        }
        if (popup.getComponentCount() > 0) {
            popup.show(tree, x, y);
        }
    }
    
}
